﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DocumentsTagger.UI;
using DocumentsTagger.UI.ViewModel;
using System.Threading;
using System.IO;
using DocumentsTagger.UI.Commands;
using DocumentsTagger.UI.Model;

namespace DocumentsTaggerUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        MainWindowViewModel vm = new MainWindowViewModel();

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = vm;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            vm.CloseApp();
        }

        private void MenuItem_Exit_Click(object sender, RoutedEventArgs e)
        {
            vm.CloseApp();
            Application.Current.Shutdown(0);
        }

        private void MenuItem_ScannerOptions_Click(object sender, RoutedEventArgs e)
        {
            ScannerOptionsWindow wnd = new ScannerOptionsWindow();
            wnd.Owner = this;
            wnd.ShowDialog();
            
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((System.Windows.Controls.ListBox)(sender)).SelectedItems.Count != 0)
            {
                this.LoadingControl.Visibility = System.Windows.Visibility.Visible;
                this.WindowsFormsHost.Visibility = System.Windows.Visibility.Collapsed;    

                vm.SelectedDocument = (DocumentsTagger.UI.Model.DocumentUIModel)(((System.Windows.Controls.ListBox)(sender)).SelectedItems[0]);
                UpdatePreview();
                vm.SelectedDocument.IsChanged = false;

                this.removeDeclinedTagMI.Command = vm.SelectedDocumentViewModel.RemoveDeclinedTagCommandProp;
                this.accepSuggestedTagMI.Command = vm.SelectedDocumentViewModel.AcceptSuggestedTagCommandProp;
                this.declineSuggestedTagMI.Command = vm.SelectedDocumentViewModel.DeclineSugestedTagCommandProp;
                this.removeAcceptedTagMI.Command = vm.SelectedDocumentViewModel.RemoveAcceptedTagCommandProp;

            } 
        }

        private void UpdatePreview()
        {
            
            if (this.WindowsFormsHost.Child != null)
            {
                this.WindowsFormsHost.Child.Dispose();
                this.WindowsFormsHost.Child = null;
            }

            string filePath = vm.SelectedDocument.Document.FullPath;
            string fileExtension = System.IO.Path.GetExtension(filePath).ToLower();

            if (!File.Exists(filePath))
            {
                (new RemoveDocumentCommand(this.vm)).Execute(vm.SelectedDocument.Document);
                return;
            }

            if (fileExtension == ".pdf")
            {
                DisplayPdf();
            }
            else if ((fileExtension == ".doc") || (fileExtension == ".docx"))
            {
                DisplayWord();
            }
            
        }

        private void DisplayWord()
        {
            var uc = new DocViewer();
            uc.ControlLoaded += new ControlLoadedHandler(uc_ControlLoaded);
            uc.LoadData(vm.SelectedDocument.Document.FullPath);
            this.WindowsFormsHost.Child = uc;
        }

        void uc_ControlLoaded()
        {
            Action action = () => {
                                    this.LoadingControl.Visibility = System.Windows.Visibility.Collapsed;
                                    this.WindowsFormsHost.Visibility = System.Windows.Visibility.Visible;
                                  };
            this.Dispatcher.Invoke(action, null);
            
        }

        private void DisplayPdf()
        {
            var uc = new PdfViewer();
            uc.ControlLoaded += new ControlLoadedHandler(uc_ControlLoaded);
            uc.LoadData(vm.SelectedDocument.Document.FullPath);
            this.WindowsFormsHost.Child = uc;
        }

        private void AddTag_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // TODO: add handling for invalid input
                this.vm.SelectedDocumentViewModel.AddedTag = tbAddTag.Text;
                if (this.vm.SelectedDocumentViewModel.AddTagToDocumentCommandProp.CanExecute(tbAddTag.Text))
                {
                    this.vm.SelectedDocumentViewModel.AddTagToDocumentCommandProp.Execute(tbAddTag.Text);
                    tbAddTag.Text = "";
                }
                e.Handled = true;
            }
        }

        private void tbAddDeclinedTag_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // TODO: add handling for invalid input
                this.vm.SelectedDocumentViewModel.AddedDeclinedTag = tbAddDeclinedTag.Text;
                if (this.vm.SelectedDocumentViewModel.AddDeclinedTagToDocumentCommandProp.CanExecute(tbAddDeclinedTag.Text))
                {
                    this.vm.SelectedDocumentViewModel.AddDeclinedTagToDocumentCommandProp.Execute(tbAddDeclinedTag.Text);
                    tbAddDeclinedTag.Text = "";
                }
                e.Handled = true;
            }
        }

        private void MenuItem_Tags_click(object sender, RoutedEventArgs e)
        {
            TagsInfoWindow wnd = new TagsInfoWindow();
            wnd.Owner = this;
            wnd.ShowDialog();
        }
    }
}
